package com.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CountDownLatchExample1 {
    private static int thread_total = 200;

    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        final CountDownLatch latch = new CountDownLatch(thread_total);
        for (int i = 0; i < thread_total; i++) {
            final int count = i;
            exec.execute(() -> {
                try {
                    process(count);
                } catch (Exception e) {
                    log.error("exception e: {}", e);
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await(10, TimeUnit.MILLISECONDS);
        exec.shutdown();
        log.info("finished...");
    }

    private static void process(int i) throws Exception{
        log.info("{}", i);
        Thread.sleep(100);
    }
}
